﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace MvcEmrRazor.Helpers
{
    public class RowVersionHelper
    {
    }
    public static class VersionConverter
    {
        public static string ToString(byte[] input)
        { 
            if (input == null) 
                return null; 
            return Convert.ToBase64String(input.ToArray()); 
        }        
        public static byte[] ToBinary(string input) 
        { 
            if (string.IsNullOrEmpty(input)) 
                return null; 
            return (Convert.FromBase64String(input));
            //LinqBinaryModelBinder
        }

    }
}
/*
LinqBinaryModelBinder
http://stephenwalther.com/blog/archive/2009/02/25/asp.net-mvc-tip-49-use-the-linqbinarymodelbinder-in-your.aspx
0
 
down vote 
accepted  My solution was to use the Tamper Proofing code from Steven Sanderson's ASP.NET MVC book. The idea is that you create a 
 * hash of any hidden form field you want to tamper proof (make sure to salt the hash):

<%= Html.Hidden("CustomerId") %> 
<%= Html.Hidden("CustomerIdHash") %> 
When the form is submitted, Steven's code then computes another hash of CustomerId and makes certain it equals CustomerIdHash. If it does, 
 * then no tampering has occurred. It's great code, and worth the price of the book.
 
*/
/*

namespace Microsoft.Web.Mvc {
    using System;
    using System.Data.Linq;
    using System.Web;
    using System.Web.Mvc;

    // Register via a call in Global.asax.cs to 
    // ModelBinders.Binders.Add(typeof(Binary), new LinqBinaryModelBinder());
    [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class LinqBinaryModelBinder : IModelBinder {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
            ValueProviderResult valueResult;
            bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out valueResult);

            // case 1: there was no <input ... /> element containing this data
            if (valueResult == null) {
                return null;
            }

            string value = valueResult.AttemptedValue;

            // case 2: there was an <input ... /> element but it was left blank
            if (String.IsNullOrEmpty(value)) {
                return null;
            }

            // Future proofing. Right now, Binary.ToString() includes quotes around 
            // the base64 encoded value. This may get fixed in the future.
            string realValue = value.Replace("\"", string.Empty);
            var byteValue = new Binary(Convert.FromBase64String(realValue));
            return byteValue;
        }
    }
}



*/